<?php

namespace Home\Controller;

use Think\Controller;
use \Model\EntityModel;
use \Model\SkillModel;
use Common\Common\UserGlobal;  //公共类

class SkillController extends CommonController
{

	//获取全部技能
	function get_all_skill()
	{
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_all_skill();
		echo json_encode($data);
	}

	//按页获取技能
	function get_skill_by_page($page)
	{
		$skillmodel = new SkillModel();
		$skill_state = 1;
		$data = $skillmodel ->model_get_skill_by_page($page,$skill_state);
		echo json_encode($data);
	} 


    // 发布技能共享
	function skill_share()
	{
		//检测用户token，token有误则直接终止
		$this->check_token($_POST['token']);

		$_POST['skill_issue_time'] = date('y-m-d H:i:s', time());

		$skillmodel = new SkillModel();
		$data = $skillmodel->create();

		if ($data) {
			$skillmodel->add($data);
			echo '{"msg":"ok"}';
		} else {
			echo $skillmodel->getError();
		}


	}
    
    //商品的详细描述
	function get_skill_detail($skill_id)
	{
		$skillmodel = new SkillModel;
		$info = $skillmodel->model_get_skill_detail($skill_id);
		echo json_encode($info);

	}
    

    //商品发布描述
	function issueDetails()
	{
		$entity = D('entity');
		//两个逻辑：展示表单、收集表单信息
		if (!empty($_POST)) {
	    //

	   		//收集表单信息
			if ($_FILES['entity_img']['error'] === 0) {
				$cfg = array(
					'rootPath' => './Public/Upload/', //保存根路径
				);
				//设置附件的存储位置
				$up = new \Think\Upload($cfg);
				//上传附件
				//如果附件上传成功，就可以通过uploadOne的返回值查看到附件在服务器的存储情况
				$z = $up->uploadOne($_FILES['entity_img']);
				dump($z);
				//附件保存到数据库中,保存路径名即可
				$bigpicname = $up->rootPath . $z['savepath'] . $z['savename'];
				$_POST['entity_big_img'] = $bigpicname;
			}
   
			$data = $entity->create();
	    	// exit;
			$z = $entity->add($data);
			if ($z) {
				$this->redirect("entityShare", array(), 2, "发布成功！");
			} else {
				$this->redirect("entityShare", array(), 2, "发布失败！");
			}
		} else {
			$this->display();
		}

	}       


	//获取用户A 通过审核的商品（发布中）
	public function get_user_pass_skill($page, $user_id) {
		$skill_state = 1;//用户通过审核的商品状态
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_user_skill_by_page($page, $user_id, $skill_state);
		echo json_encode($data);
	}


	//获取用户A 未通过审核的商品
	public function get_user_no_pass_skill($page, $user_id) {
		$skill_state = -1;//用户通过审核的商品状态
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_user_skill_by_page($page, $user_id, $skill_state);
		echo json_encode($data);
	}

	//获取用户A 待确认的商品
	public function get_user_await_confirm_skill($page, $user_id) {
		$skill_state = 2;//用户通过审核的商品状态
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_user_skill_by_page($page, $user_id, $skill_state);
		echo json_encode($data);
	}

	//获取用户A 共享中的商品
	public function get_user_shareing_skill($page, $user_id) {
		$skill_state = 3;//用户通过审核的商品状态
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_user_skill_by_page($page, $user_id, $skill_state);
		echo json_encode($data);
	}

	//获取用户A 确认完成的商品
	public function get_user_confirm_ok_skill($page, $user_id) {
		$skill_state = 4;//用户通过审核的商品状态
		$skillmodel = new SkillModel();
		$data = $skillmodel ->model_get_user_skill_by_page($page, $user_id, $skill_state);
		echo json_encode($data);
	}

	//A用户 商品下架
	public function skill_outofstock() {
		$skill_id = $_POST['skill_id'];
		$skillmodel = new SkillModel();
		if ($skillmodel->model_skill_outofstock($skill_id)) {
			echo '{"msg":"ok"}';
		} else {
			echo '{"msg":"操作失败！"}';
		}

	}

	
}